package com.github.binarywang.demo.wechat.service.impl;

import com.github.binarywang.demo.wechat.dao.CommentMapper;
import com.github.binarywang.demo.wechat.dao.CourseMapper;
import com.github.binarywang.demo.wechat.dao.FavoriteMapper;
import com.github.binarywang.demo.wechat.dao.ReferMapper;
import com.github.binarywang.demo.wechat.dao.TypeMapper;
import com.github.binarywang.demo.wechat.dao.UserMapper;
import com.github.binarywang.demo.wechat.model.Comment;
import com.github.binarywang.demo.wechat.model.CommentKey;
import com.github.binarywang.demo.wechat.model.Course;
import com.github.binarywang.demo.wechat.model.Favorite;
import com.github.binarywang.demo.wechat.model.FavoriteKey;
import com.github.binarywang.demo.wechat.model.Refer;
import com.github.binarywang.demo.wechat.model.Type;
import com.github.binarywang.demo.wechat.model.User;
import com.github.binarywang.demo.wechat.model.vo.CommentVO;
import com.github.binarywang.demo.wechat.model.vo.CourseVO;
import com.github.binarywang.demo.wechat.service.WxCourseService;
import com.github.binarywang.demo.wechat.utils.Page;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("wxCourseService")
public class WxCourseServiceImpl  implements WxCourseService
{
  @Autowired
  private CourseMapper courseMapper;
  @Autowired
  private UserMapper userMapper;
  @Autowired
  private CommentMapper commentMapper;
  @Autowired
  private TypeMapper typeMapper;
  @Autowired
  private ReferMapper referMapper;
  @Autowired
  private FavoriteMapper favoriteMapper;

  public int insert(Course course, boolean isRefer)
  {
    Type type = this.typeMapper.selectByName(course.getType());
    if (type == null) {
      type = new Type();
      type.setTypeName(course.getType());
      this.typeMapper.insert(type);
    }
    course.setType(type.getTypeId().toString());
    this.courseMapper.insert(course);
    if (isRefer) {
      Refer refer = new Refer();
      refer.setCourseId(course.getCourseId());
      refer.setCreatetime(new Date());
      refer.setReference(course.getCreater());
      this.referMapper.insert(refer);
    }
    return 0;
  }

  public int update(Course course)
  {
    Type type = this.typeMapper.selectByName(course.getType());
    if (type == null) {
      type = new Type();
      type.setTypeName(course.getType());
      this.typeMapper.insert(type);
    }
    course.setType(type.getTypeId().toString());
    return this.courseMapper.updateByPrimaryKeySelective(course);
  }

  public int delete(Integer courseId, String openId)
  {
    this.favoriteMapper.deleteByCourseId(courseId);

    this.commentMapper.deleteByCourseId(courseId);

    this.referMapper.deleteByPrimaryKey(courseId);
    return this.courseMapper.deleteByPrimaryKey(courseId);
  }

  public CourseVO findById(Integer courseId, String openId) {
    Course course = this.courseMapper.selectByPrimaryKey(courseId);
    User user = this.userMapper.selectByPrimaryKey(course.getCreater());
    List<Comment> commentList = this.commentMapper.selectByCourseId(course.getCourseId());
    List<CommentVO> commentVOList = new ArrayList<CommentVO>();
    for (Comment temp : commentList) {
      CommentVO commentVO = new CommentVO();
      BeanUtils.copyProperties(temp, commentVO);
      User user1 = this.userMapper.selectByPrimaryKey(temp.getOpenId());
      commentVO.setUser(user1);
      commentVOList.add(commentVO);
    }
    CourseVO courseVO = new CourseVO();
    BeanUtils.copyProperties(course, courseVO);
    courseVO.setUser(user);
    courseVO.setCommentList(commentVOList);
    CommentKey key = new CommentKey();
    key.setCourseId(courseId);
    key.setOpenId(openId);
    Comment hasComment = this.commentMapper.selectByPrimaryKey(key);
    if (hasComment != null) {
      courseVO.setHasComment(true);
    }
    Type type = this.typeMapper.selectByPrimaryKey(Integer.valueOf(course.getType()));
    courseVO.setTypeName(type.getTypeName());
    return courseVO;
  }

  public List<CourseVO> list(Integer start, String openId) {
    List<CourseVO> listData = new ArrayList<CourseVO>();
    List<Course> lstResult = this.courseMapper.selectAll();
    for (Course course : lstResult) {
      User user = this.userMapper.selectByPrimaryKey(course.getCreater());
      if (course.getCourseContent().length() >= 120) {
        String courseContent = course.getCourseContent().substring(0, 120) + "...";
        course.setCourseContent(courseContent);
      }
      CourseVO courseVO = new CourseVO();
      BeanUtils.copyProperties(course, courseVO);
      courseVO.setUser(user);

      List<Comment> commentList = this.commentMapper.selectByCourseId(course.getCourseId());
      courseVO.setCountComment(commentList.size());

      List<Favorite> faList = this.favoriteMapper.selectByCourseId(course.getCourseId());
      courseVO.setCountHasFav(faList.size());

      FavoriteKey favoriteKey = new FavoriteKey();
      favoriteKey.setCourseId(course.getCourseId());
      favoriteKey.setOpenId(openId);
      if (this.favoriteMapper.selectByPrimaryKey(favoriteKey) != null) {
        courseVO.setHasFav(true);
      }
      listData.add(courseVO);
    }
    listData = Page.getCurrentPage(listData, start);
    return listData;
  }

  public List<CourseVO> findByTypeId(String typeId, Integer start, String openId) {
    List<CourseVO> listData = new ArrayList<CourseVO>();
    List<Course> lstResult = this.courseMapper.findByTypeId(typeId);
    for (Course course : lstResult) {
      User user = this.userMapper.selectByPrimaryKey(course.getCreater());
      if (course.getCourseContent().length() >= 120) {
        String courseContent = course.getCourseContent().substring(0, 120) + "...";
        course.setCourseContent(courseContent);
      }
      CourseVO courseVO = new CourseVO();
      BeanUtils.copyProperties(course, courseVO);
      courseVO.setUser(user);

      List<Comment> commentList = this.commentMapper.selectByCourseId(course.getCourseId());
      courseVO.setCountComment(commentList.size());

      List<Favorite> faList = this.favoriteMapper.selectByCourseId(course.getCourseId());
      courseVO.setCountHasFav(faList.size());

      FavoriteKey favoriteKey = new FavoriteKey();
      favoriteKey.setCourseId(course.getCourseId());
      favoriteKey.setOpenId(openId);
      if (this.favoriteMapper.selectByPrimaryKey(favoriteKey) != null) {
        courseVO.setHasFav(true);
      }
      listData.add(courseVO);
    }
    listData = Page.getCurrentPage(listData, start);
    return listData;
  }

  public List<Course> refer() {
    List<Course> lstData = new ArrayList<Course>();
    List<Refer> referList = this.referMapper.selectAll();
    for (Refer refer : referList) {
      Course course = this.courseMapper.selectByPrimaryKey(refer.getCourseId());
      lstData.add(course);
    }
    return lstData;
  }

  public List<CourseVO> search(String content) {
    List<CourseVO> listData = new ArrayList<CourseVO>();
    List<Course> lstResult = this.courseMapper.selectByContent(content);
    for (Course course : lstResult) {
      if (course.getCourseContent().length() >= 120) {
        String courseContent = course.getCourseContent().substring(0, 120) + "...";
        course.setCourseContent(courseContent);
      }
      Type type = this.typeMapper.selectByPrimaryKey(Integer.valueOf(course.getType()));
      course.setType(type.getTypeName());
      CourseVO courseVO = new CourseVO();
      BeanUtils.copyProperties(course, courseVO);
      listData.add(courseVO);
    }
    return listData;
  }
}